﻿#pragma once

#include <QSettings>
#include <QString>
#include <QFile>
#include <map>
#include <QDebug>
#include <QSize>
#define INI_SEP  ","

class MySetting
	:public QSettings
{
public:
	explicit MySetting(QString strNam);

	bool IsOk()
	{
		return _ptSet->allKeys().size() > 0;
	}

	bool Read(const QString& strKey, QString* strTxt,QString defValue = "")
	{
		QVariant value = _ptSet->value(strKey, defValue);
		if (value.type() == QVariant::StringList)
		{
			*strTxt = value.toStringList().join(INI_SEP);
		}
		else
		{
			*strTxt = _ptSet->value(strKey, "").toString();
		}
		
		return *strTxt != QString("");
	}

	bool Read(const QString& strKey, int* n, int defValue = 0)
	{
		bool bOK = false;
		*n = _ptSet->value(strKey, defValue).toInt(&bOK);

		return bOK;
	}

	bool Write(const QString strKey, const QVariant& strTxt)
	{
		//if (strTxt.type() == QVariant::StringList)
		//{
		//	QStringList list = strTxt.toStringList();
		//	list << "";   // 列表后面增加一个逗号  让 wx可以识别
		//	_ptSet->setValue(strKey, list);
		//}
		//else
		//{
		//	_ptSet->setValue(strKey, strTxt);
		//}
		_ptSet->setValue(strKey, strTxt);

		return true;
	}


	~MySetting()
	{

	}

private:
	QSettings* _ptSet;
};

class CcdConfig
{
public:
	CcdConfig() {}

    QString serial;
    QString name;
    QString pix_line;
    QString pix_col;
	QString taps;
    QString color_depth;
};

class EagleConfigFile
{
	
private:
	EagleConfigFile(){};
	~EagleConfigFile(){};
	EagleConfigFile(const EagleConfigFile&){}
	EagleConfigFile& operator=(const EagleConfigFile&){}
	bool parse_ccd(MySetting &conf);

public:
	static EagleConfigFile* getInstance()
	{
		static EagleConfigFile _instance;
		return &_instance;
	}
    bool load();
	bool load1();
    void store();
	void save(QString strSection, QString strValue);
	void saveCroseWire();

public:
	QString task_name;
	QString conmunicate_method;
	QString store_method;
	QString trigger_source;
	QString data_source;
	QString trigger_type;
	QString disk_threshold;
	QString record_time;
	QString record_frame_number;
	QString record_frame_interval;
	QString export_log;//0：不检查帧号，1：检查帧号，2：输出帧号并检查帧号
	QString export_head;
	QString export_avi_level;//0：不压缩，1：压缩
	QString password;//passwrod for delete or rename...
	QString serial_port;
	
	QString local_task_scan_depth;
	QString time_decoder_format;
	QString export_bmp_type;
	
	QString disk_mount_type;
	QString network_packet_size;
	QString enable_cameralink_simulation;
	QString enable_display_framelost_signal;
	QStringList base_support_tapbits;
	QStringList medium_support_tapbits;
	QStringList full_support_tapbits;
	QStringList null_support_tapbits;
	QStringList export_types;
	QString language;
	QString debug_display_q;
	QString right_column_size;
	QStringList eth_name;
	QStringList device_sort_order;
	QStringList support_column;
	QStringList support_line;
	QStringList support_shrink_level;
	QString enable_ws_version;
	QString enable_time_replay;
	QString export_name_using_time;
	// 成光
	QString last_export_path;
	QString debug_serial_info;
    QString m_strOverlapText; // 成光 叠加字符配置
    int m_nVersion;
	// 西光所十字丝相关设置
	int m_nColor;    // 红、黑、白、绿  0，1，2，3
	int m_nShow;
	int m_nLineWidth;
	QString m_szSize; // 20*20  40*40
	QSize m_tCrossSize;

	// 异步导出线程数量 [12/14/2017 xystar]
	int m_nAsyncThreadCnt;


	std::map<QString, CcdConfig> ccd_config;/*<serial, config>*/
	std::map<QString, std::map<int, bool>> device_camera_left_right_map;//<serial, <camera, left right reverse>>
	std::map<QString, std::map<int, QString>> device_camera_decoder_name_map;//<serial, <camera, name>>
	std::map<QString, std::map<int, bool>> device_camera_up_down_map;//<serial, <camera, up down reverse>>
	std::map<QString, std::map<int, int>> device_camera_info_channel_map;//<serial, <camera, channel>>
	
	std::map<QString, std::map<int, QString>> device_camera_xgs_extraParams;  // 西光所 额外叠加数据
	
	QString	GetDeviceCameraExtraParam(int dev, int ccd_serial);
	void	SetDeviceCameraExtraParam(int device_id, int ccd_serial, QString extraParams);

	bool	GetDeviceCameraLeftRightReverse(int device_id, int ccd_serial);
	bool	GetDeviceCameraUpDownReverse(int device_id, int ccd_serial);
	QString	GetDeviceCameraDecoderName(int device_id, int ccd_serial);
	int		GetDeviceCameraInfoChannel(int device_id, int ccd_serial);

	bool	GetDeviceCameraLeftRightReverse(QString device_serial, int ccd_serial);
	bool	GetDeviceCameraUpDownReverse(QString device_serial, int ccd_serial);
	QString	GetDeviceCameraDecoderName(QString device_serial, int ccd_serial);
	int		GetDeviceCameraInfoChannel(QString device_serial, int ccd_serial);

	void	SetDeviceCameraLeftRightReverse(int device_id, int ccd_serial, bool valid);
	void	SetDeviceCameraUpDownReverse(int device_id, int ccd_serial, bool valid);
	void	SetDeviceCameraDecoderName(int device_id, int ccd_serial, QString decoder_name);
	void	SetDeviceCameraInfoChannel(int device_id, int ccd_serial,int new_channel);

	void display_config();
};
